package com.mlearning.demo.controller;

import com.alibaba.fastjson.JSON;
import com.mlearning.demo.service.GraphService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

//@RestController
@Controller
public class GraphController {
    @Autowired
    private GraphService graphService;

    @RequestMapping (value = "/API/graph")
    public void graph(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String search;
        if (request.getParameter("name") != null) {
            search = request.getParameter("name");
            response.getWriter().write(JSON.toJSONString(graphService.updateJson(search)));
        } else {
            if (request.getParameter("search") != null && !request.getParameter("search").equals("")) {
                search = request.getParameter("search");
//                System.out.println(search);
                response.getWriter().write(JSON.toJSONString(graphService.creatJson(search)));
            } else {
                search = "机器学习";
                response.getWriter().write(JSON.toJSONString(graphService.creatJson(search)));
            }
        }
    }

}
